package com.preskool.controller;

import com.preskool.dto.ActionResult;
import com.preskool.po.Library;
import com.preskool.dto.LibraryVo;
import com.preskool.serevice.LibraryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:薛亮亮
 * @Date 2021/1/8 14:37
 * @Discription:
 */
@RestController
@Api(value = "Library接口文档")
public class LibraryController {
    @Autowired
    private LibraryService libraryService;

    /**
     * 查询所有图书信息,对应的是数据库中的library表,一一对应
     * @param offset 页码
     * @param limit 每页显示条数
     * @return 返回所有的图书信息
     */
//    @RequestMapping(value = "/findLibrarys", method = RequestMethod.GET)
//    public ActionResult findLibrary(@RequestParam Integer offset, Integer limit) {
//        List<Library> librarys = libraryService.findLibrarys(offset, limit);
//        return new ActionResult(200, null, librarys);
//    }

    /**
     * 查询所有的图书信息,对应的是班级名称
     *
     * @return
     */
    @RequestMapping(value = "/findAllLibrary", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有图书馆的信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult findAllLibrary(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "2") Integer pageSize,@RequestParam(defaultValue = "lid desc") String sortName){

        List<LibraryVo> libraries = libraryService.findAllLibrary(currentPage,pageSize,sortName);
        if (libraries != null) {
            return new ActionResult(200, null, libraries);
        }
        return new ActionResult(200, "暂无图书信息!", null);

    }

    /**
     * 新增图书信息
     */
    @PutMapping("/addLibrary")
    @ApiOperation(value = "新增图书信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
//    @ApiImplicitParam(name = "Library",value = "需要添加的图书信息",required = true)
    public ActionResult addLibrary(@RequestBody Library library){
        int i = libraryService.addLibrary(library);
        if (i>0){
            return new ActionResult(200,"add success",null);
        }else if (i==-1){
            return new ActionResult(202,"请检查是否存在系或者班级",null);
        }
        return new ActionResult(201,"add failed",null);
    }

    /**
     * 删除图书信息
     * @param lid
     * @return
     */
    @DeleteMapping("/deleteLibrary")
    @ApiOperation(value = "删除图书信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public ActionResult deleteLibrary(@RequestParam Integer lid){
        System.out.println(lid);
        int i = libraryService.deleteLibrary(lid);
        if (i>0){
            return new ActionResult(200,"delete success",null);
        }else if (i==-1){
            return new ActionResult(202,"请检查是否存在系或者班级",null);
        }
        return new ActionResult(201,"delete failed",null);
    }

    /**
     * 更新图书信息
     * @param library
     * @return
     */
    @PostMapping("/updateLibrary")
    @ApiOperation(value = "更新图书信息")
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功"),
            @ApiResponse(code = 201,message = "请求失败")
    })
    public  ActionResult updateLibrary(@RequestBody Library library){
        int i = libraryService.updateLibrary(library);

            if (i>0){
                return new ActionResult(200,"update success",null);
            }else if (i==-1){
                return new ActionResult(202,"请检查是否存在系或者班级",null);
            }

        return new ActionResult(201,"update failed",null);
    }

}
